#include <unordered_map>
#include <iostream>

struct ListNode
{
    int val;
    ListNode*next;
};

ListNode* solve_deleting(ListNode*&head)
{
    // 使用哈希表记录val出现次数
    std::unordered_map<int,int>mp;
    ListNode*temp = head;
    while (temp)
    {
        if(mp.find(temp->val) == mp.end())
        {
            mp[temp->val] = 1;
        }
        else 
        {
            mp[temp->val] ++;
        }
    }
    // 定义哑节点防止头节点被删除
    ListNode*dummy = new ListNode;
    dummy->next = head;

    // 遍历删除所有出现1次以上的节点
    temp = head;
    ListNode*prior = dummy;
    while (temp)
    {
        // 判断条件并删除节点
        if (mp[temp->val] > 1)
        {
            ListNode*to_delete = temp;
            prior->next = temp->next;
            delete to_delete;
            temp = prior->next;
        }
        else 
        {
            prior = temp;
            temp = temp->next;
        }
    }
    // 返回哑节点的next节点
    return dummy->next;
}

void add_node(ListNode*&head,int val)
{
    ListNode*node = new ListNode;
    node->next = nullptr;
    node->val = val;

    if (!head)  head = node;
    else 
    {
        ListNode*temp = head;
        while (temp->next)
        {
            temp = temp->next;
        }
        temp->next = node;
    }
}

void printlist(ListNode*head)
{
    ListNode*temp = head;
    while (temp)    
    {
        std::cout<<temp->val<<"\n";
        temp = temp->next;
    }

}

int main()
{
    ListNode*head = new ListNode;
    head->val = 1;
    head->next = nullptr;

    add_node(head,1);
    add_node(head,2);
    add_node(head,3);
    add_node(head,2);

    // 调用函数了！！！！
    solve_deleting(head);
    printlist(head);
    
    // ?????????????????????????????????????????????
    return 0;
}